package com.datang.petstore.service.impl;

import com.datang.petstore.entity.*;
import com.datang.petstore.exception.AcceptResultCodeException;
import com.datang.petstore.generatemapper.MicroblogMapper;
import com.datang.petstore.mapper.MicroblogCustomMapper;
import com.datang.petstore.model.DeleteMicroblogRequest;
import com.datang.petstore.model.GetMicroblogListResponse;
import com.datang.petstore.resultpage.ResultCode;
import com.datang.petstore.service.MicroblogLabelService;
import com.datang.petstore.service.MicroblogService;
import com.datang.petstore.service.PetMoldService;
import com.datang.petstore.utils.StringUtils;
import org.apache.el.stream.Stream;
import org.assertj.core.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-10-30 17:21
 * @notify 微博接口
 * @version 1.0
 */
@Service
public class MicroblogServiceImpl implements MicroblogService {

    @Autowired
    private MicroblogMapper microblogMapper;

    @Autowired
    private PetMoldService petMoldService;

    @Autowired
    private MicroblogLabelService microblogLabelService;

    @Autowired
    private MicroblogCustomMapper microblogCustomMapper;

    /*
     * @author 顶风少年
     * @date 2019/10/30 17:37
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param microblog
     * @result com.datang.petstore.utils.Result
     * @notify 添加微博
     * version 1.0
     */
    @Transactional
    public Integer addMicroblog(Microblog microblog) {

        //查询宠物类型
        List<PetMold> petMolds = petMoldService.getAllPetMold();

        //获取宠物类型id
        List<Integer> moldIds = new ArrayList<>();
        for (PetMold petMold : petMolds) {
            moldIds.add(petMold.getId());
        }

        //没有宠物类型抛出异常
        if (!moldIds.contains(microblog.getPetMold())) {
            throw new AcceptResultCodeException(ResultCode.error504);
        }

        //拆分标签
        String labelStr = microblog.getLabel();
        if (labelStr != null && !labelStr.equals("")) {
            String[] split = labelStr.split(";");
            List<MicroblogLabel> labels = new ArrayList<>();

            //循环创建标签
            for (String name : split) {
                MicroblogLabel label1 = new MicroblogLabel();
                label1.setText(name);
                label1.setDate(new Date());
                label1.setUserId(microblog.getUserId());
                labels.add(label1);
            }
            //添加标签
            microblogLabelService.addLabels(labels);
        }

        //设置基本属性
        microblog.setLikeNumber(0);
        microblog.setCommentNumber(0);
        microblog.setTransmitNumber(0);
        microblog.setShareNumber(0);
        microblog.setInformNumber(0);
        microblog.setDate(new Date());
        microblog.setDel("0");


        //添加微博
        microblogMapper.insertSelective(microblog);
        Integer id = microblog.getId();

        return id;
    }


    /*
     * @author 顶风少年
     * @date 2019/11/1 7:40
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param id
     * @result boolean
     * @notify 根据id查询微博 不使用缓存,对某一个微博的操作过于多,点赞转发等等.
     * version 1.0
     */

    public Microblog getMicroblog(Integer id) {
        Microblog microblog = microblogMapper.selectByPrimaryKey(id);
        if (microblog == null) {
            throw new AcceptResultCodeException(ResultCode.error505);
        }else if(microblog.getDel().equals("1")){
            throw new AcceptResultCodeException(ResultCode.error512);
        }
        return microblog;
    }


    /*
     * @author 顶风少年
     * @date 2019/11/8 9:23
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param microblog
     * @result void
     * @notify 修改微博数据
     * version 1.0
     */
    @Transactional
    public void updateMicroblog(Microblog microblog) {
        microblogMapper.updateByPrimaryKeySelective(microblog);
    }


    /*
     * @author 顶风少年
     * @date 2019/11/8 10:31
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param petMold
     * @param lastId
     * @param mediaType
     * @result java.util.List<com.datang.petstore.model.GetMicroblogListResponse>
     * @notify 根据宠物类型,或资源类型,或id大小,或者标签来获取微博列表
     * version 1.0
     */
    public List<GetMicroblogListResponse> getMicroblogList(GetMicroblogListRequest getMicroblogListRequest) {

        ArrayList<String> clabels = null;
        String label = getMicroblogListRequest.getLabel();
        if (!StringUtils.isNUll(label)) {
            clabels = new ArrayList<>();
            String[] labels = label.split(";");
            List<Object> labelList = Arrays.asList(labels);
            for (Object o : labelList) {
                clabels.add(o + ";");
            }
        }
        getMicroblogListRequest.setLabelList(clabels);
        List<GetMicroblogListResponse> getMicroblogListResponses = microblogCustomMapper.GetMicroblogList(getMicroblogListRequest);
        return getMicroblogListResponses;
    }

}
